﻿using DGFramework;
using ETModel;
using NPOI.Util;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using System.Threading.Tasks;
using DG.Tweening;
using NUnit.Framework.Internal.Execution;

namespace ETHotfix
{
    public class UICustomsComponent : Component
    {
        private UIGamePageComponent UigamePage;



        #region//关卡



        private Button Btn_GoUpAndDown;
        private Button Btn_EnterTheMap;
        private Button Btn_ExitMapInterface;
        //private Button Btn_UpgradePage;
        private Button Btn_zhangshiditu;
        private GameObject Obj_BuildingButtonBack;
        private GameObject Obj_ConstructionSIte;
        private GameObject Obj_zhangshiditu;


        private Text Txt_Maphongdian;

        #endregion




        public ReferenceCollector CheckpointsResources;
        public void OnHuoqu()
        {
            UI ui = this.GetParent<UI>();
            UigamePage = ui.GetComponent<UIGamePageComponent>();
            Log.Debug("name-----------------===" + UigamePage.GameObject);
            CheckpointsResources = this.GetParent<UI>().GameObject.transform.Find("CheckpointsResources").GetComponent<ReferenceCollector>();


            Btn_GoUpAndDown = CheckpointsResources.Get<GameObject>("Btn_GoUpAndDown").GetComponent<Button>();
            Btn_GoUpAndDown.onClick.Add(OnBtnXiajiang);//启动

            Btn_EnterTheMap = CheckpointsResources.Get<GameObject>("Btn_EnterTheMap").GetComponent<Button>();
            Btn_EnterTheMap.onClick.Add(OnBtnGuanka);

            Btn_ExitMapInterface = CheckpointsResources.Get<GameObject>("Btn_ExitMapInterface").GetComponent<Button>();
            Btn_ExitMapInterface.onClick.Add(OnBtnExitGuanka);

            //Btn_UpgradePage= GamePageRc.Get<GameObject>("Btn_UpgradePage").GetComponent<Button>();
            //Btn_UpgradePage.onClick.Add(OnBtnShengjijz);


            Btn_zhangshiditu = CheckpointsResources.Get<GameObject>("Btn_zhangshiditu").GetComponent<Button>();
            Btn_zhangshiditu.onClick.Add(ExirXianshiditu);
            Obj_BuildingButtonBack = CheckpointsResources.Get<GameObject>("Obj_BuildingButtonBack");
            Obj_ConstructionSIte = CheckpointsResources.Get<GameObject>("Obj_ConstructionSIte");
            Obj_zhangshiditu = CheckpointsResources.Get<GameObject>("Obj_zhangshiditu");

           
            Txt_Maphongdian = CheckpointsResources.Get<GameObject>("Txt_Maphongdian").GetComponent<Text>();
            for (int TheCurrentDigital = 0; TheCurrentDigital < 5; TheCurrentDigital++)
            {
                int numbers = TheCurrentDigital;
                // Obj_BuildingButtonBack.transform.GetChild(TheCurrentDigital).Find("Btn_shengji").GetComponent<Button>().onClick.Add(() => { Startaupguanka(numbers); });
                Obj_BuildingButtonBack.transform.GetChild(TheCurrentDigital).GetComponent<Button>().onClick.Add(() => { Startaupguanka(numbers); });
            }

          

            GetMaps().Coroutine();






        }

    

        //获取地图
        public async ETVoid GetMaps()
        {
            try
            {
                Log.Debug("==============开始获取地图");
                G2C_GetMaps g2CGetMaps = (G2C_GetMaps)await SessionComponent.Instance.Session.Call(new C2G_GetMaps());//发送消息收到回调
                if (g2CGetMaps.Error != ErrorCode.ERR_Success) //不成功
                {
                    Log.Debug("==========================不成功");
                    return;
                }
                Log.Debug("==============成功");
                GuankaInfo(g2CGetMaps);
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }


        private int mapHongdianInt;
        private int[] jdshwe = new int[5];
        //建造界面当前信息
        private void GuankaInfo(G2C_GetMaps getmapsinfo)
        {
            Log.Debug("==============!成功huidiao");
            Log.Debug("==============!成功huidiao"+ UigamePage.GameObject);
            Log.Debug("==============!成功huidiao" + UigamePage.Txt_CurrentMapName);
            UigamePage.Txt_CurrentMapName.text = "当前关卡" + (getmapsinfo.Level+1);
            mapHongdianInt = 0;
            shengchengditu(getmapsinfo.Level+1);
            for (int TheCurrentDigital = 0; TheCurrentDigital < 5; TheCurrentDigital++)
            {
                if (getmapsinfo.Levs[TheCurrentDigital] > 0)
                {
                    Obj_ConstructionSIte.transform.Find("j_" + (TheCurrentDigital + 1)).GetComponent<Image>().color = new Color(1, 1, 1, 1);
                    Obj_ConstructionSIte.transform.Find("j_" + (TheCurrentDigital + 1)).GetComponent<Image>().sprite = CheckpointsResources.Get<Sprite>((1) + "_" + (getmapsinfo.Levs[TheCurrentDigital]) + "_" + (TheCurrentDigital + 1));
                }
                else
                {

                    Obj_ConstructionSIte.transform.Find("j_" + (TheCurrentDigital + 1)).GetComponent<Image>().color = new Color(1, 1, 1, 0.01f);
                }
                Log.Debug("当前建筑物+"+TheCurrentDigital+"     最大等级为" + getmapsinfo.MaxLevs[TheCurrentDigital]);
                if (getmapsinfo.Levs[TheCurrentDigital] < getmapsinfo.MaxLevs[TheCurrentDigital])//建筑物被攻击掉级特效
                {
                    Obj_ConstructionSIte.transform.Find("x_" + (TheCurrentDigital + 1)).transform.GetChild(1).gameObject.SetActive(true);
                }
                else
                {
                    Obj_ConstructionSIte.transform.Find("x_" + (TheCurrentDigital + 1)).transform.GetChild(1).gameObject.SetActive(false);
                }


                if (getmapsinfo.Levs[TheCurrentDigital] != 5) //如果建筑物等级
                {
                    Obj_BuildingButtonBack.transform.GetChild(TheCurrentDigital).GetComponent<Button>().enabled = true;//升级建筑按钮
                    Obj_BuildingButtonBack.transform.GetChild(TheCurrentDigital).Find("Txt_goid").gameObject.SetActive(true);
                        Obj_BuildingButtonBack.transform.GetChild(TheCurrentDigital).Find("Image").GetComponent<Image>().sprite = CheckpointsResources.Get<Sprite>("0"+(1) + "_" + (getmapsinfo.Levs[TheCurrentDigital]+1) + "_" + (TheCurrentDigital+1));
                        Log.Debug("当前建筑等级========" + getmapsinfo.Levs[TheCurrentDigital]);
                    Obj_BuildingButtonBack.transform.GetChild(TheCurrentDigital).Find("Txt_Lv").gameObject.SetActive(true);
                    Obj_BuildingButtonBack.transform.GetChild(TheCurrentDigital).Find("Txt_goid").gameObject.SetActive(true);
                    Obj_BuildingButtonBack.transform.GetChild(TheCurrentDigital).Find("Image1").gameObject.SetActive(false);
                }
                else
                {
                    Obj_BuildingButtonBack.transform.GetChild(TheCurrentDigital).Find("Image").GetComponent<Image>().sprite = CheckpointsResources.Get<Sprite>("0" + (1) + "_" + (getmapsinfo.Levs[TheCurrentDigital] ) + "_" + (TheCurrentDigital + 1));
                    Obj_BuildingButtonBack.transform.GetChild(TheCurrentDigital).GetComponent<Button>().enabled = false;
                    Obj_BuildingButtonBack.transform.GetChild(TheCurrentDigital).Find("Txt_Lv").gameObject.SetActive(false);
                    Obj_BuildingButtonBack.transform.GetChild(TheCurrentDigital).Find("Txt_goid").gameObject.SetActive(false);
                    Obj_BuildingButtonBack.transform.GetChild(TheCurrentDigital).Find("Image1").gameObject.SetActive(true);
                }
                if (getmapsinfo.GoldCoins[TheCurrentDigital] > UigamePage.Number_Of_Gold_COINS)
                {
                    Obj_BuildingButtonBack.transform.GetChild(TheCurrentDigital).GetComponent<Button>().enabled = false;
                    Obj_BuildingButtonBack.transform.GetChild(TheCurrentDigital).Find("Image").GetComponent<Image>().material = UigamePage.ShaderFei;
                    Obj_BuildingButtonBack.transform.GetChild(TheCurrentDigital).Find("Txt_Lv").GetComponent<Text>().text = "<color=#434343>" + getmapsinfo.Levs[TheCurrentDigital] + "/5" + "</color>";
                    Obj_BuildingButtonBack.transform.GetChild(TheCurrentDigital).Find("Txt_goid").GetComponent<Text>().text = "<color=#e5e5e5>" + UigamePage.Numdispose(getmapsinfo.GoldCoins[TheCurrentDigital].ToString()) + "</color>";//建筑需要的金钱
                    Obj_BuildingButtonBack.transform.GetChild(TheCurrentDigital).Find("Txt_goid").GetComponent<Outline>().effectColor = new Color((67 / 255f), (67 / 255f), (67 / 255f), (128 / 255f));    
                }
                else
                {
                    Obj_BuildingButtonBack.transform.GetChild(TheCurrentDigital).GetComponent<Button>().enabled = true;
                    Obj_BuildingButtonBack.transform.GetChild(TheCurrentDigital).Find("Image").GetComponent<Image>().material = null;
                  


                    Obj_BuildingButtonBack.transform.GetChild(TheCurrentDigital).Find("Txt_goid").GetComponent<Text>().text = "<color=#fdef31>" + UigamePage.Numdispose(getmapsinfo.GoldCoins[TheCurrentDigital].ToString()) + "</color>";//建筑需要的金钱

                    Obj_BuildingButtonBack.transform.GetChild(TheCurrentDigital).Find("Txt_Lv").GetComponent<Text>().text = "<color=#8a4a1f>" + getmapsinfo.Levs[TheCurrentDigital] + "/5"  + "</color>";
                    Obj_BuildingButtonBack.transform.GetChild(TheCurrentDigital).Find("Txt_goid").GetComponent<Outline>().effectColor = new Color((187 / 255f), (99 / 255f), (20 / 255f), (128 / 255f));
                }
              jdshwe[TheCurrentDigital] = getmapsinfo.GoldCoins[TheCurrentDigital];
            }

           
            if (UigamePage.Number_Of_Gold_COINS> jdshwe[0])
            {
                 mapHongdianInt = 1;
            }
            if (UigamePage.Number_Of_Gold_COINS > jdshwe[0]+ jdshwe[1])
            {
                mapHongdianInt = 2;
            }
            if (UigamePage.Number_Of_Gold_COINS > jdshwe[0] + jdshwe[1] + jdshwe[2])
            {
                mapHongdianInt = 3;
            }
            if (UigamePage.Number_Of_Gold_COINS > jdshwe[0] + jdshwe[1] + jdshwe[2] + jdshwe[3])
            {
                mapHongdianInt = 4;
            }
            if (UigamePage.Number_Of_Gold_COINS > jdshwe[0] + jdshwe[1] + jdshwe[2] + jdshwe[3] + jdshwe[4])
            {
                mapHongdianInt = 5;
            }
            Txt_Maphongdian.text = mapHongdianInt.ToString();
        }


        private int DangqianGuankaNumber;
        private void shengchengditu(int value) 
        {
            DangqianGuankaNumber = value;
            Log.Debug("当前管卡==========================" + DangqianGuankaNumber);
            ScrollViewEx sve = UigamePage.Obj_ScrollMap.GetComponent<ScrollViewEx>();
            sve.SetDataCount(20);
            sve.OnSetItem = OndituItem;
            sve.ReSetScrollView();
        }
        private void OndituItem(Transform tra, int idex)
        {
            Log.Debug("当卡==========================" + idex);
            if (idex % 2 == 0)
            {
                tra.GetChild(0).gameObject.SetActive(true);
                tra.GetChild(1).gameObject.SetActive(false);
            }
            else
            {
                tra.GetChild(0).gameObject.SetActive(false);
                tra.GetChild(1).gameObject.SetActive(true);
            }
            tra.Find("1/TheMapBackground/ditu").GetComponent<Image>().sprite = CheckpointsResources.Get<Sprite>("01_1"); //CheckpointsResources.Get<Sprite>("0" + (idex + 1) + "_"+(idex + 1));
            tra.Find("1/TheMapBackground/ditu").GetComponent<Button>().onClick.RemoveAllListeners();
            tra.Find("1/TheMapBackground/ditu").GetComponent<Button>().onClick.Add(() => { Xianshiditu(idex); });
            tra.Find("2/TheMapBackground/ditu").GetComponent<Image>().sprite = CheckpointsResources.Get<Sprite>("01_1"); //CheckpointsResources.Get<Sprite>("0" + (idex + 1) + "_"+(idex + 1));
            tra.Find("2/TheMapBackground/ditu").GetComponent<Button>().onClick.RemoveAllListeners();
            tra.Find("2/TheMapBackground/ditu").GetComponent<Button>().onClick.Add(() => { Xianshiditu(idex); });
            if (idex< DangqianGuankaNumber)
            {
                Log.Debug("当卡------------=========" + idex);
                tra.Find("1/TheMapBackground/ditu").GetComponent<Image>().material =null;
                tra.Find("1/TheMapBackground/ditu").GetComponent<Button>().enabled = true;
                tra.Find("2/TheMapBackground/ditu").GetComponent<Image>().material = null;
                tra.Find("2/TheMapBackground/ditu").GetComponent<Button>().enabled = true;


            }
            else
            {
                Log.Debug("当+++++++++++++++++++++++++++===" + idex);
                tra.Find("1/TheMapBackground/ditu").GetComponent<Image>().material = UigamePage.ShaderFei;
                tra.Find("1/TheMapBackground/ditu").GetComponent<Button>().enabled = false;
                tra.Find("2/TheMapBackground/ditu").GetComponent<Image>().material = UigamePage.ShaderFei;
                tra.Find("2/TheMapBackground/ditu").GetComponent<Button>().enabled = false;


            }
            tra.gameObject.SetActive(true);
            tra.localScale = new Vector3(1, 1, 1);


        }
        private void Xianshiditu(int value) 
        {
            Obj_zhangshiditu.GetComponent<Image>().sprite = CheckpointsResources.Get<Sprite>("001_1");//CheckpointsResources.Get<Sprite>("00"+value+"_"+value);
            Obj_zhangshiditu.gameObject.SetActive(true);
        }
        private void ExirXianshiditu() 
        {
            Obj_zhangshiditu.gameObject.SetActive(false);
        }
        private void OnBtnGuanka()
        {
            UigamePage.Obj_TheMapPage.SetActive(true);
            UigamePage.Obj_TheMapPage.transform.localScale = new Vector3(1, 1, 1);
            UigamePage.Obj_BottomOfThePage.SetActive(false);
            UigamePage.Obj_Overhead .SetActive(false);
            ExitXingxing();
            UigamePage.RangStop();
        }
        private void ExitXingxing() 
        {
            for (int TheCurrentDigital = 0; TheCurrentDigital < 5; TheCurrentDigital++)
            {
                Obj_ConstructionSIte.transform.Find("Obj_Guankatexiao" + TheCurrentDigital).gameObject.SetActive(false);

            }
        }
        private void OnBtnExitGuanka()
        {

            UigamePage.Obj_TheMapPage.SetActive(false);
            UigamePage.Obj_BottomOfThePage.SetActive(true);
            UigamePage.Obj_Overhead .SetActive(true);
            UigamePage.RangStart();
        }
        public bool OnbenxiajiangBool = false;//建筑升级面板是否在上面  62  362
        public void OnBtnXiajiang()
        {
            OnbenxiajiangBool = !OnbenxiajiangBool;
            if (OnbenxiajiangBool)
            {
                UigamePage.Img_ConstructionBack.transform.DOLocalMove(new Vector3(UigamePage.Img_ConstructionBack.transform.localPosition.x, -600, 0), 0.5f);
                Btn_GoUpAndDown.transform.GetChild(0).GetComponent<Image>().sprite = CheckpointsResources.Get<Sprite>("Guankashangsheng_8");
            }
            else
            {
                UigamePage.Img_ConstructionBack.transform.DOLocalMove(new Vector3(UigamePage.Img_ConstructionBack.transform.localPosition.x, -900, 0), 0.5f);
                Btn_GoUpAndDown.transform.GetChild(0).GetComponent<Image>().sprite = CheckpointsResources.Get<Sprite>("Guankashangsheng_7");
            }



        }

        public void OnBtnShengjijz() 
        {
            UigamePage.Img_ConstructionBack.gameObject.SetActive(true);
        }

        //点击升级
        private void Startaupguanka(int value)
        {
            Log.Debug("获得的点" + value);
            for (int TheCurrentDigital = 0; TheCurrentDigital < 5; TheCurrentDigital++)
            {
                //  Obj_BuildingButtonBack.transform.GetChild(TheCurrentDigital).Find("Btn_shengji").GetComponent<Button>().enabled = false;
                Obj_BuildingButtonBack.transform.GetChild(TheCurrentDigital).GetComponent<Button>().enabled = false;
            }
            
            StartUolv(value).Coroutine();
            
       
            
         
        }

        private async ETVoid StartUolv(int value)
        {
            try
            {
                Log.Debug("发送得点是" + value);
                C2G_UpgradeMaps upgrademaps = new C2G_UpgradeMaps();
                upgrademaps.Index = value;
                G2C_GetMaps g2getmaps = (G2C_GetMaps)await SessionComponent.Instance.Session.Call(upgrademaps);//发送消息收到回调
                if (g2getmaps.Error != ErrorCode.ERR_Success) //不成功
                {
                    Log.Debug("发送消息不成功");
                    for (int TheCurrentDigital = 0; TheCurrentDigital < 5; TheCurrentDigital++)
                    {
                        if (g2getmaps.Levs[TheCurrentDigital] <5)
                        {
                            // Obj_BuildingButtonBack.transform.GetChild(TheCurrentDigital).Find("Btn_shengji").GetComponent<Button>().enabled = true;
                            Obj_BuildingButtonBack.transform.GetChild(TheCurrentDigital).GetComponent<Button>().enabled = true;
                        }
                       
                    }
                    return;
                }
                for (int TheCurrentDigital = 0; TheCurrentDigital < g2getmaps.Levs.Count; TheCurrentDigital++)
                {
                    if (g2getmaps.Levs[TheCurrentDigital]<5)
                    {
                        if (g2getmaps.GoldCoins[TheCurrentDigital] < UigamePage.Number_Of_Gold_COINS)
                        {
                            //Obj_BuildingButtonBack.transform.GetChild(TheCurrentDigital).Find("Btn_shengji").GetComponent<Button>().enabled = true;
                            Obj_BuildingButtonBack.transform.GetChild(TheCurrentDigital).GetComponent<Button>().enabled = true;
                        }

                    }
                    else
                    {
                        Obj_BuildingButtonBack.transform.GetChild(TheCurrentDigital).GetComponent<Button>().enabled = false;
                    }
                }
                Log.Debug("成功升级消耗金币"+ g2getmaps.SubGoldCoins  +"   当前金币"+ UigamePage.Number_Of_Gold_COINS);
                UigamePage.Number_Of_Gold_COINS -= g2getmaps.SubGoldCoins;
                UigamePage.Txt_HisGoldCOINS.text = UigamePage.Numdispose( UigamePage.Number_Of_Gold_COINS.ToString());
                Log.Debug("还剩多少金币" + UigamePage.Number_Of_Gold_COINS);
                Obj_ConstructionSIte.transform.Find("x_" + (value + 1)).transform.GetChild(0).gameObject.SetActive(false);
                Obj_ConstructionSIte.transform.Find("x_" + (value + 1)).transform.GetChild(2).gameObject.SetActive(false);
                Uplvkasync(g2getmaps, value);

                 
            }
            catch (Exception e)
            {
                Log.Error(e);
            }

        }
        public async void Uplvkasync(G2C_GetMaps upgrademaps, int value)
        {
         
            Obj_ConstructionSIte.transform.Find("x_" + (value + 1)).transform.GetChild(0).gameObject.SetActive(true);
            await ETModel.Game.Scene.GetComponent<TimerComponent>().WaitAsync(600);//1000是一秒 当家园建筑开始建造时，在对应建筑脚下播放粒子“home_building”，延迟0.6s
            Obj_ConstructionSIte.transform.Find("x_" + (value + 1)).transform.GetChild(1).gameObject.SetActive(false);
            Obj_ConstructionSIte.transform.Find("j_" + (value + 1)).transform.localScale = new Vector3(1.2f, 1.2f, 1);
            await ETModel.Game.Scene.GetComponent<TimerComponent>().WaitAsync(100);
            Obj_ConstructionSIte.transform.Find("j_" + (value + 1)).transform.localScale = new Vector3(1, 1, 1);
            GuankaInfo(upgrademaps);
            Obj_ConstructionSIte.transform.Find("x_" + (value + 1)).transform.GetChild(1).gameObject.SetActive(false);
            Obj_ConstructionSIte.transform.Find("j_" + (value + 1)).transform.localScale = new Vector3(0, 0, 1);
            for (int TheCurrentDigital = 1; TheCurrentDigital < 13; TheCurrentDigital++)
            {
                Obj_ConstructionSIte.transform.Find("j_" + (value + 1)).transform.localScale = new Vector3(TheCurrentDigital/(float)10, TheCurrentDigital / (float)10, 1);
                await ETModel.Game.Scene.GetComponent<TimerComponent>().WaitAsync(50);
            }
            Obj_ConstructionSIte.transform.Find("j_" + (value + 1)).transform.localScale = new Vector3(1, 1, 1);
            Obj_ConstructionSIte.transform.Find("x_" + (value + 1)).transform.GetChild(2).gameObject.SetActive(true);
            if (upgrademaps.IsUp ==1)//获得星星
                {

                    Obj_ConstructionSIte.transform.Find("Obj_Guankatexiao" + value).localPosition = Obj_ConstructionSIte.transform.Find("j_" + (value + 1)).transform.localPosition;
                    Obj_ConstructionSIte.transform.Find("Obj_Guankatexiao" + value).gameObject.SetActive(true);
                    Obj_ConstructionSIte.transform.Find("Obj_Guankatexiao" + value).transform.DOLocalMove(new Vector3(-189, 975, 0), 1f);

                }   
            if (upgrademaps.IsUp == 1)
            {
                await ETModel.Game.Scene.GetComponent<TimerComponent>().WaitAsync(700);//1000是一秒
                UigamePage.Obj_MyNumberOfStars.SetActive(false);
                UigamePage.Obj_ConstructionOfTheSpecialEffects.SetActive(false);
                UigamePage.Obj_ConstructionOfTheSpecialEffects.SetActive(true);

                AudioManagerP._instance.PlayAudio(1, "xiujianwancheng1");
                await ETModel.Game.Scene.GetComponent<TimerComponent>().WaitAsync(300);//1000是一秒
                Obj_ConstructionSIte.transform.Find("Obj_Guankatexiao" + value).gameObject.SetActive(false);
                await ETModel.Game.Scene.GetComponent<TimerComponent>().WaitAsync(1000);//1000是一秒

                AudioManagerP._instance.PlayAudio(1, "xingxingshengji");
                UigamePage.Obj_MyNumberOfStars.SetActive(true);
                UigamePage.Number_Of_Stars += 1;
                UigamePage.Txt_NumberOfStars.text = UigamePage.Number_Of_Stars.ToString();
            }
            Log.Debug("升级完成");
         
        }

    }
}
